% Expected bidder surplus of entering listing
% Before learning valuation
% Expectation over Biddersurplusgivenn (over n=m)
% Function called by "lambdastarfunc"
% Corresponds to equations 8 (r=0) and 10 (r>0) in the paper
% ---------------------------------------------- %
function[Pib]= Pibidder(cP,screeningvalue,v0bar,nstar,pibmat,pib0vec,pdfmat,v0vec,zeroreserve,PAR)

    maxn           = length(pibmat(:,1));
    Pibgrid        = zeros(maxn+1,1);

    if zeroreserve == 0
        % Values of V0 between screeningvalue - v0bar
        probncompeting = pdf('pois',0:maxn-1,nstar).*(1-PAR.sharenobidders); 

        minv0 = max(1,length(v0vec(v0vec<=screeningvalue)));
        maxv0 = max(1,length(v0vec(v0vec<=v0bar)));
        pibf      = @(n) sum(pibmat(n,minv0:maxv0))/sum(pdfmat(n,minv0:maxv0));
        for i = 0:maxn-1 
            Pibgrid(i+1)   = pibf(i+1)*probncompeting(i+1);
        end
        
    else
        % Values of V0 between minv - screeningvalue
        % But bidder surplus independent of V0 (reserve=0)
        probncompeting = pdf('pois',0:maxn-1,nstar); %

        for i = 0:maxn-1 
            Pibgrid(i+1)  = pib0vec(i+1)*probncompeting(i+1);
        end

    end
    Pib   = sum(Pibgrid)/sum(probncompeting) - cP;
   
end

